from new_test_framework.utils import tdLog, tdSql, sc, clusterComCheck


class TestNormalTableAlter4:

    def setup_class(cls):
        tdLog.debug(f"start to execute {__file__}")

    def test_normal_table_alter_4(self):
        """Alter: repeatedly add

        1. Add column
        2. Insert data
        3. Project query
        4. Loop for 7 times
        5. Kill then restart

        Since: v3.0.0.0

        Labels: common,ci

        Jira: None

        History:
            - 2025-8-12 Simon Guan Migrated from tsim/stable/alter_insert1.sim

        """

        tdLog.info(f"======== step1")
        tdSql.prepare("d3", drop=True)
        tdSql.execute(f"use d3")
        tdSql.execute(f"create table tb (ts timestamp, a int)")
        tdSql.execute(f"insert into tb values(now-28d, -28)")
        tdSql.query(f"select * from tb order by ts desc")

        tdSql.checkRows(1)
        tdSql.checkData(0, 1, -28)

        tdLog.info(f"======== step2")
        tdSql.execute(f"alter table tb add column b smallint")
        tdSql.execute(f"insert into tb values(now-25d, -25, 0)")
        tdSql.query(f"select * from tb order by ts desc")

        tdSql.checkRows(2)

        tdSql.checkData(0, 1, -25)
        tdSql.checkData(1, 1, -28)
        tdSql.checkData(0, 2, 0)
        tdSql.checkData(1, 2, None)

        tdLog.info(f"======== step3")
        tdSql.execute(f"alter table tb add column c tinyint")
        tdSql.execute(f"insert into tb values(now-22d, -22, 3, 0)")
        tdSql.query(f"select * from tb order by ts asc")

        tdSql.checkRows(3)
        tdSql.checkData(0, 1, -28)
        tdSql.checkData(0, 2, None)
        tdSql.checkData(0, 3, None)
        tdSql.checkData(1, 1, -25)
        tdSql.checkData(1, 2, 0)
        tdSql.checkData(1, 3, None)
        tdSql.checkData(2, 1, -22)
        tdSql.checkData(2, 2, 3)
        tdSql.checkData(2, 3, 0)

        tdLog.info(f"======== step4")
        tdSql.execute(f"alter table tb add column d int")
        tdSql.execute(f"insert into tb values(now-19d, -19, 6, 3, 0)")
        tdSql.query(f"select * from tb order by ts asc")
        tdSql.checkRows(4)
        tdSql.checkData(0, 1, -28)
        tdSql.checkData(0, 2, None)
        tdSql.checkData(0, 3, None)
        tdSql.checkData(0, 4, None)
        tdSql.checkData(1, 1, -25)
        tdSql.checkData(1, 2, 0)
        tdSql.checkData(1, 3, None)
        tdSql.checkData(1, 4, None)
        tdSql.checkData(2, 1, -22)
        tdSql.checkData(2, 2, 3)
        tdSql.checkData(2, 3, 0)
        tdSql.checkData(2, 4, None)
        tdSql.checkData(3, 1, -19)
        tdSql.checkData(3, 2, 6)
        tdSql.checkData(3, 3, 3)
        tdSql.checkData(3, 4, 0)

        tdLog.info(f"======== step5")
        tdSql.execute(f"alter table tb add column e bigint")
        tdSql.execute(f"insert into tb values(now-16d, -16, 9, 5, 4, 3)")
        tdSql.query(f"select count(e) from tb")
        tdSql.checkData(0, 0, 1)

        tdSql.query(f"select count(c) from tb")
        tdSql.checkData(0, 0, 3)

        tdSql.query(f"select * from tb order by ts asc")
        tdSql.checkRows(5)

        tdSql.checkData(0, 1, -28)
        tdSql.checkData(0, 2, None)
        tdSql.checkData(0, 3, None)
        tdSql.checkData(0, 4, None)
        tdSql.checkData(0, 5, None)
        tdSql.checkData(1, 1, -25)
        tdSql.checkData(1, 2, 0)
        tdSql.checkData(1, 3, None)
        tdSql.checkData(1, 4, None)
        tdSql.checkData(1, 5, None)
        tdSql.checkData(2, 1, -22)
        tdSql.checkData(2, 2, 3)
        tdSql.checkData(2, 3, 0)
        tdSql.checkData(2, 4, None)
        tdSql.checkData(2, 5, None)
        tdSql.checkData(3, 1, -19)
        tdSql.checkData(3, 2, 6)
        tdSql.checkData(3, 3, 3)
        tdSql.checkData(3, 4, 0)
        tdSql.checkData(3, 5, None)
        tdSql.checkData(4, 1, -16)
        tdSql.checkData(4, 2, 9)
        tdSql.checkData(4, 3, 5)
        tdSql.checkData(4, 4, 4)
        tdSql.checkData(4, 5, 3)

        tdLog.info(f"======== step6")
        tdSql.execute(f"alter table tb add column f float")
        tdSql.execute(f"insert into tb values(now-13d, -13, 12, 11, 10, 9, 8)")
        tdSql.query(f"select * from tb order by ts asc")
        tdSql.checkRows(6)
        tdSql.checkData(0, 1, -28)
        tdSql.checkData(0, 2, None)
        tdSql.checkData(0, 3, None)
        tdSql.checkData(0, 4, None)
        tdSql.checkData(0, 5, None)
        tdSql.checkData(0, 6, None)

        tdSql.checkData(1, 1, -25)
        tdSql.checkData(1, 2, 0)
        tdSql.checkData(1, 3, None)
        tdSql.checkData(1, 4, None)
        tdSql.checkData(1, 5, None)
        tdSql.checkData(1, 6, None)

        tdSql.checkData(2, 1, -22)
        tdSql.checkData(2, 2, 3)
        tdSql.checkData(2, 3, 0)
        tdSql.checkData(2, 4, None)
        tdSql.checkData(2, 5, None)
        tdSql.checkData(2, 6, None)

        tdSql.checkData(3, 1, -19)
        tdSql.checkData(3, 2, 6)
        tdSql.checkData(3, 3, 3)
        tdSql.checkData(3, 4, 0)
        tdSql.checkData(3, 5, None)
        tdSql.checkData(3, 6, None)

        tdSql.checkData(4, 1, -16)
        tdSql.checkData(4, 2, 9)
        tdSql.checkData(4, 3, 5)
        tdSql.checkData(4, 4, 4)
        tdSql.checkData(4, 5, 3)
        tdSql.checkData(4, 6, None)

        tdSql.checkData(5, 1, -13)
        tdSql.checkData(5, 2, 12)
        tdSql.checkData(5, 3, 11)
        tdSql.checkData(5, 4, 10)
        tdSql.checkData(5, 5, 9)
        tdSql.checkData(5, 6, 8.00000)

        tdLog.info(f"======== step7")
        tdSql.execute(f"alter table tb add column g double")
        tdSql.execute(f"insert into tb values(now-10d, -10, 15, 14, 13, 12, 11, 10)")
        tdSql.query(f"select * from tb order by ts asc")
        tdSql.checkRows(7)
        tdSql.checkData(0, 1, -28)
        tdSql.checkData(0, 2, None)
        tdSql.checkData(0, 3, None)
        tdSql.checkData(0, 4, None)
        tdSql.checkData(0, 5, None)
        tdSql.checkData(0, 6, None)
        tdSql.checkData(0, 7, None)

        tdSql.checkData(1, 1, -25)
        tdSql.checkData(1, 2, 0)
        tdSql.checkData(1, 3, None)
        tdSql.checkData(1, 4, None)
        tdSql.checkData(1, 5, None)
        tdSql.checkData(1, 6, None)
        tdSql.checkData(1, 7, None)

        tdSql.checkData(2, 1, -22)
        tdSql.checkData(2, 2, 3)
        tdSql.checkData(2, 3, 0)
        tdSql.checkData(2, 4, None)
        tdSql.checkData(2, 5, None)
        tdSql.checkData(2, 6, None)
        tdSql.checkData(2, 7, None)

        tdSql.checkData(3, 1, -19)
        tdSql.checkData(3, 2, 6)
        tdSql.checkData(3, 3, 3)
        tdSql.checkData(3, 4, 0)
        tdSql.checkData(3, 5, None)
        tdSql.checkData(3, 6, None)
        tdSql.checkData(3, 7, None)

        tdSql.checkData(4, 1, -16)
        tdSql.checkData(4, 2, 9)
        tdSql.checkData(4, 3, 5)
        tdSql.checkData(4, 4, 4)
        tdSql.checkData(4, 5, 3)
        tdSql.checkData(4, 6, None)
        tdSql.checkData(4, 7, None)

        tdSql.checkData(5, 1, -13)
        tdSql.checkData(5, 2, 12)
        tdSql.checkData(5, 3, 11)
        tdSql.checkData(5, 4, 10)
        tdSql.checkData(5, 5, 9)
        tdSql.checkData(5, 6, 8.00000)
        tdSql.checkData(5, 7, None)

        tdSql.checkData(6, 1, -10)
        tdSql.checkData(6, 2, 15)
        tdSql.checkData(6, 3, 14)
        tdSql.checkData(6, 4, 13)
        tdSql.checkData(6, 5, 12)
        tdSql.checkData(6, 6, 11.00000)
        tdSql.checkData(6, 7, 10.000000000)

        tdLog.info(f"======== step8")
        tdSql.execute(f"alter table tb add column h binary(10)")
        tdSql.execute(
            f"insert into tb values(now-7d, -7, 18, 17, 16, 15, 14, 13, '11')"
        )
        tdSql.query(f"select * from tb order by ts asc")
        tdSql.checkRows(8)

        tdSql.checkData(0, 1, -28)
        tdSql.checkData(0, 2, None)
        tdSql.checkData(0, 3, None)
        tdSql.checkData(0, 4, None)
        tdSql.checkData(0, 5, None)
        tdSql.checkData(0, 6, None)
        tdSql.checkData(0, 7, None)
        tdSql.checkData(0, 8, None)

        tdSql.checkData(1, 1, -25)
        tdSql.checkData(1, 2, 0)
        tdSql.checkData(1, 3, None)
        tdSql.checkData(1, 4, None)
        tdSql.checkData(1, 5, None)
        tdSql.checkData(1, 6, None)
        tdSql.checkData(1, 7, None)
        tdSql.checkData(1, 8, None)

        tdSql.checkData(2, 1, -22)
        tdSql.checkData(2, 2, 3)
        tdSql.checkData(2, 3, 0)
        tdSql.checkData(2, 4, None)
        tdSql.checkData(2, 5, None)
        tdSql.checkData(2, 6, None)
        tdSql.checkData(2, 7, None)
        tdSql.checkData(2, 8, None)

        tdSql.checkData(3, 1, -19)
        tdSql.checkData(3, 2, 6)
        tdSql.checkData(3, 3, 3)
        tdSql.checkData(3, 4, 0)
        tdSql.checkData(3, 5, None)
        tdSql.checkData(3, 6, None)
        tdSql.checkData(3, 7, None)
        tdSql.checkData(3, 8, None)

        tdSql.checkData(4, 1, -16)
        tdSql.checkData(4, 2, 9)
        tdSql.checkData(4, 3, 5)
        tdSql.checkData(4, 4, 4)
        tdSql.checkData(4, 5, 3)
        tdSql.checkData(4, 6, None)
        tdSql.checkData(4, 7, None)
        tdSql.checkData(4, 8, None)

        tdSql.checkData(5, 1, -13)
        tdSql.checkData(5, 2, 12)
        tdSql.checkData(5, 3, 11)
        tdSql.checkData(5, 4, 10)
        tdSql.checkData(5, 5, 9)
        tdSql.checkData(5, 6, 8.00000)
        tdSql.checkData(5, 7, None)
        tdSql.checkData(5, 8, None)

        tdSql.checkData(6, 1, -10)
        tdSql.checkData(6, 2, 15)
        tdSql.checkData(6, 3, 14)
        tdSql.checkData(6, 4, 13)
        tdSql.checkData(6, 5, 12)
        tdSql.checkData(6, 6, 11.00000)
        tdSql.checkData(6, 7, 10.000000000)
        tdSql.checkData(6, 8, None)

        tdSql.checkData(7, 1, -7)
        tdSql.checkData(7, 2, 18)
        tdSql.checkData(7, 3, 17)
        tdSql.checkData(7, 4, 16)
        tdSql.checkData(7, 5, 15)
        tdSql.checkData(7, 6, 14.00000)
        tdSql.checkData(7, 7, 13.000000000)
        tdSql.checkData(7, 8, 11)

        tdLog.info(f"======== step9")
        sc.dnodeStop(1)
        sc.dnodeStart(1)
        clusterComCheck.checkDnodes(1)

        tdSql.query(f"select * from tb order by ts asc")
        tdSql.checkRows(8)

        tdSql.checkData(0, 1, -28)
        tdSql.checkData(0, 2, None)
        tdSql.checkData(0, 3, None)
        tdSql.checkData(0, 4, None)
        tdSql.checkData(0, 5, None)
        tdSql.checkData(0, 6, None)
        tdSql.checkData(0, 7, None)
        tdSql.checkData(0, 8, None)

        tdSql.checkData(1, 1, -25)
        tdSql.checkData(1, 2, 0)
        tdSql.checkData(1, 3, None)
        tdSql.checkData(1, 4, None)
        tdSql.checkData(1, 5, None)
        tdSql.checkData(1, 6, None)
        tdSql.checkData(1, 7, None)
        tdSql.checkData(1, 8, None)

        tdSql.checkData(2, 1, -22)
        tdSql.checkData(2, 2, 3)
        tdSql.checkData(2, 3, 0)
        tdSql.checkData(2, 4, None)
        tdSql.checkData(2, 5, None)
        tdSql.checkData(2, 6, None)
        tdSql.checkData(2, 7, None)
        tdSql.checkData(2, 8, None)

        tdSql.checkData(3, 1, -19)
        tdSql.checkData(3, 2, 6)
        tdSql.checkData(3, 3, 3)
        tdSql.checkData(3, 4, 0)
        tdSql.checkData(3, 5, None)
        tdSql.checkData(3, 6, None)
        tdSql.checkData(3, 7, None)
        tdSql.checkData(3, 8, None)

        tdSql.checkData(4, 1, -16)
        tdSql.checkData(4, 2, 9)
        tdSql.checkData(4, 3, 5)
        tdSql.checkData(4, 4, 4)
        tdSql.checkData(4, 5, 3)
        tdSql.checkData(4, 6, None)
        tdSql.checkData(4, 7, None)
        tdSql.checkData(4, 8, None)

        tdSql.checkData(5, 1, -13)
        tdSql.checkData(5, 2, 12)
        tdSql.checkData(5, 3, 11)
        tdSql.checkData(5, 4, 10)
        tdSql.checkData(5, 5, 9)
        tdSql.checkData(5, 6, 8.00000)
        tdSql.checkData(5, 7, None)
        tdSql.checkData(5, 8, None)

        tdSql.checkData(6, 1, -10)
        tdSql.checkData(6, 2, 15)
        tdSql.checkData(6, 3, 14)
        tdSql.checkData(6, 4, 13)
        tdSql.checkData(6, 5, 12)
        tdSql.checkData(6, 6, 11.00000)
        tdSql.checkData(6, 7, 10.000000000)
        tdSql.checkData(6, 8, None)

        tdSql.checkData(7, 1, -7)
        tdSql.checkData(7, 2, 18)
        tdSql.checkData(7, 3, 17)
        tdSql.checkData(7, 4, 16)
        tdSql.checkData(7, 5, 15)
        tdSql.checkData(7, 6, 14.00000)
        tdSql.checkData(7, 7, 13.000000000)
        tdSql.checkData(7, 8, 11)

        tdSql.checkCols(9)
